<?php
##############################################
#
#   菜单配置
# @ Time：2016-12-9 - 上午12:11:44
# @ Author ：JiangYongWei <1085562810@qq.com>
# @ Encoding ：UTF-8 language ：PHP
#
##############################################

namespace Admin\Controller;
use Admin\Model\UserGroupModel;
use Admin\Model\AdminBaseModel;

class UserGroupController extends AdminBaseController {
	
	protected $_groupObject;
	
	public function _initialize(){
		parent::_initialize();
		$this -> _groupObject  = new UserGroupModel();
	}
	
	/**
	 * 用户组列表
	 */
	public function index(){
		$data = $this -> _groupObject -> getUserGroup();
 		$this -> assign('data', $data);
		$this -> display('index');
	}
	
	/**
	 * 添加用户组
	 */
	public function addUserGroup(){
		if(!IS_POST) return;
		$data['title'] = I('post.title');
		if(empty($data['title'])){
			$this -> error('数据填写不完整');
			exit;
		}
		//$data['pid'] = isset($_POST['parent_id']) ? intval($_POST['parent_id']) : 0;
		$result = $this -> _groupObject -> addUserGroup($data);
		if($result)
			$this -> success('添加成功', U('index'),1);
		else
			$this -> error('操作失败'); 
	}
	
	/**
	 * 删除用户组
	 */
	public function deleteUserGroup(){
		if(!IS_AJAX) return;
		$result = $this -> _groupObject -> deleteUserGroup($_GET['id']);
		if($result)
			$this -> ajaxReturn(array('status' => 1, 'message' => '删除成功！'));
		else
			$this -> ajaxReturn(array('status' => 2, 'message' => '操作失败'));
	}
	
	/**
	 * 修改用户组
	 */
	public function editUserGroup(){
		$data = array('id' => I('get.id'), 'title' => I('post.title'));
		$result = $this -> _groupObject -> editUserGroup($data);
		if($result)
			$this -> success('修改成功', U('index'),1);
		else
			$this -> error('操作失败');
	}
	
	/**
	 * 分配权限
	 */
	public function ruleGroup(){
		if(IS_GET){
			$id = I('get.id');
			// 获取用户组数据
			$group_data = $this -> _groupObject -> getUserGroupData($id);
			//var_dump($group_data);
			
			$group_data['rules'] = explode(',', $group_data['rules']);
			// 获取规则数据
			$admin_base_model = new AdminBaseModel('auth_rule');
			$rule_data = $admin_base_model -> getTreeDataLevel('level','id','title');
			$assign = array('group_data' => $group_data, 'rule_data' => $rule_data);
			$this -> assign($assign);
			$this -> display('rule');
		}else if(IS_POST){
			$data = I('post.');
			$map = array('id' => $data['id']);
			$admin_base_model = new AdminBaseModel('auth_group');
			$data['rules'] = implode(',', $data['rule_ids']);
			$result = $admin_base_model -> editData($map, $data);
			if($result !== false)
				$this -> success('操作成功', U('ruleGroup', $map));
			else
				$this -> error('操作失败');
		}
	}
	
}